package com.fgsort;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JOptionPane;
 
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;

public class ExcelWriteListFile {

	public void ExcelWriteListToFile(List<List<List<String>>> lstFunctionGroupsSheet, String sExcelFile) {

		try {
			File exlFile = new File(sExcelFile);
			WritableWorkbook writableWorkbook = Workbook.createWorkbook(exlFile);

            for (int sheet = 0; sheet < lstFunctionGroupsSheet.size(); sheet++) {
    			WritableSheet writableSheet = writableWorkbook.createSheet("PLC" + Integer.toString(sheet+1), sheet);
            	for (int column = 0; column < lstFunctionGroupsSheet.get(sheet).size(); column++) {
	                for (int row = 0; row < lstFunctionGroupsSheet.get(sheet).get(column).size(); row++) {
	                	//System.out.println(lstFunctionGroupsTable.get(column).get(row));
	                	Label label = new Label(column, row, lstFunctionGroupsSheet.get(sheet).get(column).get(row));
	                	///add the created cells to the sheet
	                	writableSheet.addCell(label);
	                }
	            }
            }
			///Write and close the workbook
			writableWorkbook.write();
			writableWorkbook.close();
			
			///open the file after creation
			Desktop.getDesktop().open(new File(sExcelFile));

		} catch (IOException e) {
			//e.printStackTrace();
			JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"The excel file: " + sExcelFile + " cannot be written to.\nEither it " +
					"is already open or you must be administrator to write to this location.\nTry running iTool as Admin","Information",JOptionPane.ERROR_MESSAGE);
			
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
        	
		}
	}


}

